package com.example.qqfragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    public String[] btnTitles = new String[]{"消息","小世界", "联系人","动态"};
    public List<Fragment> contextFragments = new ArrayList<>();
    public LinearLayout linearLayout;
    private ImageView messageImageView;
    private MessageFragment messageFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }



    public void init(){
        initButton();
        initMessageFragment();
        initFragment();

    }

    private void initMessageFragment() {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        messageFragment = new MessageFragment();
        transaction.add(R.id.messageFrameLayout,messageFragment);
        transaction.commit();
        transaction.show(messageFragment);
    }


    private void initFragment() {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();

        //添加按钮对于的Fragment
        for(String btnStr:btnTitles){
            ContextFragment contextFragment=new ContextFragment();

            transaction.add(R.id.contextFrameLayout,contextFragment,btnStr);
            Bundle bundle=new Bundle();
            bundle.putString("textValue",btnStr);
            contextFragment.setArguments(bundle);
            contextFragments.add(contextFragment);

        }
        transaction.commit();
        showFragment(btnTitles[0]);
    }

    public void showFragment(String tag) {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        if(tag.equals("消息")){
            transaction.show(messageFragment);
        }

        for(Fragment fragment:contextFragments){
            if(fragment.getTag().equals(tag)){
                transaction.show(fragment);
            }else{
                transaction.hide(fragment);
            }
        }
        transaction.commit();
    }

    private void initButton() {
        linearLayout=findViewById(R.id.buttonLayout);
        for(String btnStr:btnTitles){
            Button btn=new Button(this);
            btn.setText(btnStr);
            btn.setTag(btnStr);//存放Tag,值为按钮标题文本
            //设置按钮样式
            btn.setBackgroundColor(Color.WHITE);
            LinearLayout.LayoutParams btnLayoutParams=new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1);
            //添加点击事件
            btn.setOnClickListener(this);
            //将按钮加入LinearLayout
            linearLayout.addView(btn,btnLayoutParams);
        }
        
    }

    @Override
    public void onClick(View view) {
        showFragment(view.getTag().toString());
        
    }
}